package cn.spring.ly.page;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 继承 Page
 * 
 * @author acer
 *
 * @param <T>
 */
public class PageInfo<T> extends Page<T> {

	public PageInfo() {
		super();
	}

	public PageInfo(long current, long size) {
		super(current, size);
	}

	/**
	 * 计算是否有上一页
	 * 
	 * @return
	 */
	public boolean getHasPrevPage() {

		return getCurrent() - 1 > 0;
	}

	/**
	 * 计算是否有下一页
	 * 
	 * @return
	 */
	public boolean getHasNextPage() {
		return getCurrent() + 1 <= getPages();
	}

	public long getPrevPage() {
		return getCurrent() - 1;
	}

	public long getNextPage() {
		return getCurrent() + 1;
	}

	/**
	 * 总页
	 * @return
	 */
	public Long getTotalPage() {
		long p = getTotal() / getSize();
		if (getTotal() % getSize() == 0) {
			return p;
		} else {
			return p + 1;
		}
	}
	
	private int groupPageCount = 8;//
	private List<Long> groupPage = new ArrayList<Long>();// 显示组页数

	/**
	 * 显示一组的页数
	 * 
	 * @return
	 */
	public List<Long> getGroupPage() {

		// 定义起始页 - 结束页
		long startPage = 1, endPage = this.groupPageCount;

		// 在当前页之前几页
		long firstPage = groupPageCount / 2;
		// 在当前页之后几页
		long lastPage = groupPageCount - firstPage;

		// 计算起始页
		if ((this.getCurrent() - firstPage) > 0 && this.getPages() - this.getCurrent() >= lastPage) {
			startPage = this.getCurrent() - firstPage + 1;
		} else if ((this.getCurrent() - firstPage) > 0 && this.getPages() - this.getCurrent() < lastPage) {
			startPage = this.getPages() - this.groupPageCount + 1;
		}

		// 重新计算防止出现为负
		if (startPage < 1) {
			startPage = 1;
		}

		// 计算结束页
		if (this.getPages() - this.getCurrent() >= lastPage && startPage == 1
				&& this.getPages() >= this.groupPageCount) {
			endPage = this.groupPageCount;
		} else if (this.getPages() - this.getCurrent() >= lastPage && this.getPages() >= this.groupPageCount) {
			endPage = this.getCurrent() + lastPage;
		} else {
			endPage = this.getPages();
		}

		// 从起始到结束添加集合
		for (long i = startPage; i <= endPage; i++) {
			groupPage.add(i);
		}

		return groupPage;
	}

	public void setGroupPage(List<Long> groupPage) {
		this.groupPage = groupPage;
	}

	
}
